<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>README - SmartSearch</title>

<link href="./fonts.css" rel="stylesheet">
<link href="./rdoc.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
</script>

<script src="./js/jquery.js"></script>
<script src="./js/navigation.js"></script>
<script src="./js/search_index.js"></script>
<script src="./js/search.js"></script>
<script src="./js/searcher.js"></script>
<script src="./js/darkfish.js"></script>


<body id="top" role="document" class="file">
<nav role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2>
    <a href="./index.html" rel="home">Home</a>
  </h2>

  <div id="table-of-contents-navigation">
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#label-SmartSearch">SmartSearch</a>
    <li><a href="#label-How+to+use">How to use</a>
    <li><a href="#label-Adding+default+conditions">Adding default conditions</a>
    <li><a href="#label-Adding+search+tags+column">Adding search tags column</a>
    <li><a href="#label-Example%3A">Example:</a>
    <li><a href="#label-See+example+code+below%3A">See example code below:</a>
    <li><a href="#label-Some+other+examples">Some other examples</a>
    <li><a href="#label-New+Similarity+Feature%21">New Similarity Feature!</a>
    <li><a href="#label-TODO">TODO</a>
    <li><a href="#label-License">License</a>
  </ul>
</div>


  <div id="project-metadata">
    <div id="fileindex-section" class="nav-section">
  <h3>Pages</h3>

  <ul class="link-list">
  
    <li><a href="./README_md.html">README</a>
  
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-label="Page README.md">

<p><a href="https://travis-ci.org/florianeck/smart_search"><img
src="https://travis-ci.org/florianeck/smart_search.png?branch=master"></a></p>

<h1 id="label-SmartSearch"><a href="SmartSearch.html">SmartSearch</a><span><a href="#label-SmartSearch">&para;</a> <a href="#documentation">&uarr;</a></span></h1>

<p>A simple search plug-in which provides a full-text search for ActiveRecord
Models. It builds the search tags based upon the attributes you define in
yout model.</p>

<h2 id="label-How+to+use">How to use<span><a href="#label-How+to+use">&para;</a> <a href="#documentation">&uarr;</a></span></h2>

<p>First run  rake smart_search:install:migrations</p>

<p>To use smart_search, just add the following line to your model:</p>

<pre class="ruby"><span class="ruby-identifier">smart_search</span> :<span class="ruby-identifier">on</span> =<span class="ruby-operator">&gt;</span> [<span class="ruby-operator">-</span> <span class="ruby-identifier">define</span> <span class="ruby-identifier">attributes</span> <span class="ruby-identifier">here</span><span class="ruby-operator">-</span>]
</pre>

<p>smart_searched models will automatically update their search tags after
save. To set search tags for all rows in database, use
Moldel.set_search_index</p>

<p>Find entries by using:</p>

<pre>Model.find_by_tags(&quot;your search tags&quot;)</pre>

<h3 id="label-Adding+default+conditions">Adding default conditions<span><a href="#label-Adding+default+conditions">&para;</a> <a href="#documentation">&uarr;</a></span></h3>

<p>To add default conditions:  smart_search :on =&gt; [- define attributes
here-], :conditions =&gt; “- SQL Conditions here -”</p>

<p>Will only find matching tags and the given condition</p>

<h3 id="label-Adding+search+tags+column">Adding search tags column<span><a href="#label-Adding+search+tags+column">&para;</a> <a href="#documentation">&uarr;</a></span></h3>

<p>smart_search automatically adds the &#39;search_tags&#39; column to your
model. This happens when initializing the model.</p>

<p>Make sure to restart the server after adding column, otherwise,
ActiveRecord wont recognize it.</p>

<h3 id="label-Example%3A">Example:<span><a href="#label-Example%3A">&para;</a> <a href="#documentation">&uarr;</a></span></h3>

<p>Lets say you have a Customer model which has the columns
&#39;first_name&#39;, &#39;last_name&#39; and &#39;email&#39; and also
belongs to a User, who also has the column &#39;name&#39;.</p>

<p>To build your search for Customer, add:</p>

<pre class="ruby"><span class="ruby-identifier">smart_search</span> :<span class="ruby-identifier">on</span> =<span class="ruby-operator">&gt;</span> [:<span class="ruby-identifier">first_name</span>, :<span class="ruby-identifier">last_name</span>, :<span class="ruby-identifier">email</span>, <span class="ruby-string">&#39;user.name&#39;</span>]
</pre>

<p>to your model. For accessing columns of the model, use symbols, for
accessing methods use strings.</p>

<h4 id="label-See+example+code+below%3A">See example code below:<span><a href="#label-See+example+code+below%3A">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">User</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>

  <span class="ruby-identifier">belongs_to</span> :<span class="ruby-identifier">office</span>, :<span class="ruby-identifier">class_name</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;Office&quot;</span>, :<span class="ruby-identifier">foreign_key</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;office_id&quot;</span>

  <span class="ruby-identifier">smart_search</span> :<span class="ruby-identifier">on</span> =<span class="ruby-operator">&gt;</span> [:<span class="ruby-identifier">full_name</span>, <span class="ruby-string">&#39;office.name&#39;</span>]

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">full_name</span>
    <span class="ruby-node">&quot;#{self.first_name} #{self.last_name}&quot;</span>
  <span class="ruby-keyword">end</span>  
<span class="ruby-keyword">end</span>
</pre>

<p>Saving an User with first_name = “Test” and last_name = “User” the created
search tags would be “test user”. User.find_by_tags(“Test User”) would find
the User. If the User belongs to an office with name “Headquarter”, the
search tags would be “test user headquarter”. User.find_by_tags(“Test
Headquarter”) would find the User.</p>

<pre class="ruby"><span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by_tags</span>(<span class="ruby-string">&quot;Test&quot;</span>, :<span class="ruby-identifier">conditions</span> =<span class="ruby-operator">&gt;</span> {:<span class="ruby-identifier">office_id</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-value">1</span>})
</pre>

<p>Also works as expected:-)</p>

<h4 id="label-Some+other+examples">Some other examples<span><a href="#label-Some+other+examples">&para;</a> <a href="#documentation">&uarr;</a></span></h4>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Customer</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">smart_search</span> :<span class="ruby-identifier">on</span> =<span class="ruby-operator">&gt;</span> [:<span class="ruby-identifier">first_name</span>, :<span class="ruby-identifier">last_name</span>, <span class="ruby-string">&#39;user.full_name&#39;</span>, <span class="ruby-string">&#39;user.office.name&#39;</span>, :<span class="ruby-identifier">birthday</span>], :<span class="ruby-identifier">conditions</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-string">&quot;is_private = 0&quot;</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">user</span>
    <span class="ruby-constant">User</span>.<span class="ruby-identifier">find</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">user_id</span>)
  <span class="ruby-keyword">end</span>  

<span class="ruby-keyword">end</span>        

<span class="ruby-keyword">class</span> <span class="ruby-constant">Office</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>
  <span class="ruby-identifier">smart_search</span> :<span class="ruby-identifier">on</span> =<span class="ruby-operator">&gt;</span> [:<span class="ruby-identifier">name</span>, :<span class="ruby-identifier">user_names</span>]

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">user_names</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">users</span>.<span class="ruby-identifier">map</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">u</span><span class="ruby-operator">|</span> <span class="ruby-identifier">u</span>.<span class="ruby-identifier">full_name</span> }.<span class="ruby-identifier">join</span>(<span class="ruby-string">&quot; &quot;</span>)
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">users</span>
    <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_all_by_office_id</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">id</span>)
  <span class="ruby-keyword">end</span>    
<span class="ruby-keyword">end</span>
</pre>

<h2 id="label-New+Similarity+Feature%21">New Similarity Feature!<span><a href="#label-New+Similarity+Feature%21">&para;</a> <a href="#documentation">&uarr;</a></span></h2>

<p>This is still very experimental. THis gem now allows to build up a table
with word similarities and combine your search query with other words you
might be looking for.</p>

<p>See class &#39;SmartSimilarty&#39; for more infos…</p>

<p>Documentation coming soon</p>

<p>Added rake tasks for loading similarity data</p>

<pre>rake smart_search:similarity_from_file           # Load similarity data from file - Use FILE=path/to/file to specify file
rake smart_search:similarity_from_query_history  # Load similarity data from query history
rake smart_search:similarity_from_url            # Load similarity data from url - Use URL=http://.../ to specify url - Requires &#39;curl&#39;</pre>

<p>These will fill the similarity table with word similarity data.</p>

<h3 id="label-TODO">TODO<span><a href="#label-TODO">&para;</a> <a href="#documentation">&uarr;</a></span></h3>
<ul><li>
<p>Maybe add a search controller, and some views for quick starting…</p>
</li><li>
<p>Documentation for similarities</p>
</li></ul>

<p>Florian Eck it-support@friends-systems.de</p>

<h2 id="label-License">License<span><a href="#label-License">&para;</a> <a href="#documentation">&uarr;</a></span></h2>

<p>The MIT License (MIT)</p>

<p>Copyright © [2014] [Florian Eck]</p>

<p>Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the “Software”),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:</p>

<p>The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.</p>

<p>THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.</p>
</main>



<footer id="validator-badges" role="contentinfo">
  <p><a href="http://validator.w3.org/check/referer">Validate</a>
  <p>Generated by <a href="http://rdoc.rubyforge.org">RDoc</a> 4.1.1.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>

